<?php

namespace catchAdmin\model\wechat;

use EasyWeChat\Factory;
use think\facade\Config;

class MiniProgramApp extends \EasyWeChat\MiniProgram\Application
{

    private $app_id = '';
    private $secret = '';
    private $app    = null;


    public function __construct()
    {
        $config = [
            'app_id'        => Config::get('wechat.MiniProgramApp.app_id'),
            'secret'        => Config::get('wechat.MiniProgramApp.secret'),
            // 下面为可选项
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',//
//            'log' => [
//                'level' => 'debug',
//                'file' => __DIR__.'/wechat.log',
//            ],
        ];

        $this->app = Factory::miniProgram($config);

        return $this->app;
    }


    public function getPhone($param)
    {
        $res = $this->app->encryptor->decryptData($param['session_key'], $param['iv'], $param['encryptedData']);

        return $res['phoneNumber'];
    }


    public function getUser($code)
    {
        $res = $this->app->auth->session($code);

        return $res;
    }

}
